<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Rate
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: install.php 2010-08-02 16:05 ermek $
 * @author     Ermek
 */

/**
 * @category   Application_Extensions
 * @package    Rate
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Rate_Installer extends Engine_Package_Installer_Module {
	public function onPreInstall() {
		parent::onPreInstall ();
		
		$db = $this->getDb ();
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$select = $db->select ()->from ( 'engine4_core_modules' )->where ( 'name = ?', 'hecore' )->where ( 'enabled = ?', 1 );
		
		$hecore = $db->fetchRow ( $select );
		
		if (! $hecore) {
			$error_message = $translate->_ ( 'Error! This plugin requires Hire-Experts Core module. It is free module and can be downloaded from Hire-Experts.com' );
			return $this->_error ( $error_message );
		}
		
		if (version_compare ( $hecore ['version'], '4.1.1' ) < 0) {
			$error_message = $translate->_ ( 'This plugin requires Hire-Experts Core Module. We found that you has old version of Core module, please download latest version of Hire-Experts Core Module and install. Note: Core module is free.' );
			return $this->_error ( $error_message );
		}
		
		$operation = $this->_databaseOperationType;
		$module_name = $this->getOperation ()->getTargetPackage ()->getName ();
		
		$select = $db->select ()->from ( 'engine4_hecore_modules' )->where ( 'name = ?', $module_name );
		
		$module = $db->fetchRow ( $select );
		
		if ($operation == 'install') {
			
			if ($module && $module ['installed']) {
				return;
			}
			
			// Nulled by TrioxX
			$db = Engine_Db_Table::getDefaultAdapter ();
			
			$db->query ( "CREATE TABLE IF NOT EXISTS `engine4_rate_rates` (
		  `rate_id` int(11) NOT NULL auto_increment,
		  `object_type` varchar(255) default 'quiz',
		  `object_id` int(11) NOT NULL,
		  `user_id` int(11) NOT NULL,
		  `score` tinyint(3) unsigned zerofill default NULL,
		  `rated_date` datetime NOT NULL,
		  PRIMARY KEY  (`rate_id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
		  ('rate_admin_main', 'rate', 'Ratings', '', '{\"route\":\"admin_default\",\"module\":\"rate\",\"controller\":\"settings\"}', 'core_admin_main_plugins', '', 999),
		  ('rate_admin_main_settings', 'rate', 'Global Settings', '', '{\"route\":\"admin_default\",\"module\":\"rate\",\"controller\":\"settings\"}', 'rate_admin_main', '', 1),
		  ('rate_admin_main_level', 'rate', 'Member Level Settings', '', '{\"route\":\"admin_default\",\"module\":\"rate\",\"controller\":\"level\"}', 'rate_admin_main', '', 2),
		  ('rate_admin_main_review', 'rate', 'RATE_REVIEW_MENUITEM', 'Rate_Plugin_Menus::canReviewManage', '{\"route\":\"admin_default\",\"module\":\"rate\",\"controller\":\"types\"}', 'rate_admin_main', '', 3),
		  ('rate_admin_main_faq', 'rate', 'FAQ', '', '{\"route\":\"admin_default\",\"module\":\"rate\",\"controller\":\"faq\"}', 'rate_admin_main', '', 4);" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_authorization_permissions` (`level_id`, `type`, `name`, `value`, `params`) VALUES
		  (1, 'rate', 'enabled', 1, NULL),
		  (2, 'rate', 'enabled', 1, NULL),
		  (3, 'rate', 'enabled', 1, NULL),
		  (4, 'rate', 'enabled', 1, NULL),
		  (5, 'rate', 'enabled', 0, NULL);" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES 
		  ('rate.album.photo.enabled', '1'),
		  ('rate.album.photo.max.items', '5'),
		  ('rate.album.photo.min.votes', '1'),
		  ('rate.blog.enabled', '1'),
		  ('rate.blog.max.items', '5'),
		  ('rate.blog.min.votes', '1'),
		  ('rate.article.enabled', '1'),
		  ('rate.article.max.items', '5'),
		  ('rate.article.min.votes', '1'),
		  ('rate.event.max.items', '5'),
		  ('rate.event.min.votes', '1'),
		  ('rate.group.max.items', '5'),
		  ('rate.group.min.votes', '1'),
		  ('rate.user.max.items', '5'),
		  ('rate.user.min.votes', '1');" );
			
			$db->query ( "DELETE FROM `engine4_core_content` WHERE `name` = 'rate.profile-rate';" );
			$db->query ( "INSERT INTO `engine4_core_content` (`content_id`, `page_id`, `type`, `name`, `parent_content_id`, `order`, `params`, `attribs`) VALUES (NULL, 4, 'widget', 'rate.profile-rate', 411, 20, '{\"title\":\"Most Rated Profiles\",\"titleCount\":\"true\"}', NULL);" );
			
			$db->query ( "DELETE FROM `engine4_core_content` WHERE `name` = 'rate.widget-rate';" );
			$db->query ( "INSERT INTO `engine4_core_content` (`content_id`, `page_id`, `type`, `name`, `parent_content_id`, `order`, `params`, `attribs`) VALUES (NULL, 5, 'widget', 'rate.widget-rate', 510, 7, '{\"title\":\"Rate This\",\"titleCount\":\"true\"}', NULL);" );
			
			$db->query ( "CREATE TABLE IF NOT EXISTS `engine4_rate_types` (
		  `type_id` int(11) NOT NULL auto_increment,
		  `category_id` int(11) NOT NULL default '0',
		  `label` varchar(50) collate utf8_unicode_ci NOT NULL,
		  `order` int(11) NOT NULL default '0',
		  PRIMARY KEY  (`type_id`),
		  UNIQUE KEY `category_id` (`category_id`,`order`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" );
			
			$db->query ( "CREATE TABLE IF NOT EXISTS `engine4_rate_votes` (
		  `vote_id` int(11) NOT NULL auto_increment,
		  `type_id` int(11) NOT NULL default '0',
		  `review_id` int(11) NOT NULL default '0',
		  `page_id` int(11) NOT NULL default '0',
		  `rating` int(11) NOT NULL default '0',
		  `creation_date` date NOT NULL,
		  PRIMARY KEY  (`vote_id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" );
			
			$db->query ( "CREATE TABLE IF NOT EXISTS `engine4_rate_pagereviews` (
		  `pagereview_id` int(11) NOT NULL auto_increment,
		  `page_id` int(11) NOT NULL default '0',
		  `user_id` int(11) default '0',
		  `title` varchar(90) collate utf8_unicode_ci NOT NULL,
		  `body` text collate utf8_unicode_ci NOT NULL,
		  `creation_date` datetime default NULL,
		  `modified_date` datetime default NULL,
		  PRIMARY KEY  (`pagereview_id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_activity_actiontypes` VALUES
		('pagereview_new', 'rate', '{item:\$subject} posted review {var:\$link}: {body:\$body}', 1, 3, 2, 1, 1, 1);" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_activity_notificationtypes` (`type`, `module`, `body`, `is_request`, `handler`) VALUES
		('post_pagereview', 'activity', '{item:\$subject} posted review {item:\$object:\$label}.', 0, ''),
		('rated', 'activity', '{item:\$subject} rated {item:\$object:\$label}.', 0, '');" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_core_mailtemplates` (`type`, `module`, `vars`) VALUES
		('notify_post_pagereview', 'activity', '[host],[email],[recipient_title],[object_title],[object_link],[object_parent_title],[object_parent_link],[sender_title],[sender_link]');" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_authorization_permissions`
		  SELECT
			level_id as `level_id`,
			'rate' as `type`,
			'reviewcreate' as `name`,
			1 as `value`,
			NULL as `params`
		  FROM `engine4_authorization_levels`;" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_authorization_permissions`
		  SELECT
			level_id as `level_id`,
			'rate' as `type`,
			'reviewteamremove' as `name`,
			1 as `value`,
			NULL as `params`
		  FROM `engine4_authorization_levels`;" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_authorization_permissions`
		  SELECT
			level_id as `level_id`,
			'rate' as `type`,
			'rateenabled' as `name`,
			1 as `value`,
			NULL as `params`
		  FROM `engine4_authorization_levels`;" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES
		('rate.reviewteamremove', '1');" );
		} elseif ($operation == 'upgrade') {
			$result = true;
		} else { //if ($operation == 'refresh'){
			$result = true;
		}
	}
	
	function onInstall() {
		parent::onInstall ();
		
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'page' )->limit ( 1 );
		
		$core_module = $select->query ()->fetchObject ();
		
		if ($core_module && $core_module->name) {
			$db->query ( "INSERT IGNORE INTO `engine4_page_modules` (`name`, `widget`, `order`, `params`) " . "VALUES ('rate', 'rate.page-review', 16, '{\"title\":\"RATE_REVIEW_TABITEM\", \"titleCount\":true}');" );
			
			$db->query ( "INSERT IGNORE INTO `engine4_rate_types` (`category_id`, `label`, `order`) " . "SELECT `option_id` AS `category_id`, 'Rate' AS `label`, 1 AS `order` " . "FROM `engine4_page_fields_options` " . "WHERE `field_id` = 1;" );
		}
	}
}